<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_kas_bank_coa extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('keu_kas_bank_coa_model', 'KASBANK');
		$this->load->model('combo_model', 'Combo');
		
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Kas & Bank COA";
		viewByTemplate('keu_kas_bank_coa/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->KASBANK->GetJSON();
	}
	
		
	function create()
	{
		$data['title'] = "Tambah Kas & Bank COA";
		$data['data'] = NULL;
		$data['data']['tahun'] = date('Y');
		
		$data['mode'] = "create";
		viewByTemplate('keu_kas_bank_coa/form', $data, 'form');
	}

	function edit()
	{
		$data['title'] = "Ubah Kas & Bank COA";
		$data['data'] = $this->KASBANK->GetSingleView(get('id_coa'));
		
		$data['mode'] = "edit";
		viewByTemplate('keu_kas_bank_coa/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		
		// Validation
		if (post("id_coa") == '') {
			$error->Add("id_coa", "Akun", $tpl_empty);
		}
		
		if (post("keterangan") == '') {
			$error->Add("keterangan", "Keterangan", $tpl_empty);
		}
		
		
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('id_coa', 'kd_bank', 'keterangan'));
				
				
				
				if (post("mode") == "create")
				{
					$data['create_date']		= NOW;
					$data['create_user']		= USERID;

					$this->KASBANK->Add($data);

					$ls_response = "Data Tersimpan";
				}
				else if (post("mode") == "edit")
				{
					$data['modify_date']		= NOW;
					$data['modify_user']		= USERID;
					
					$this->KASBANK->Update($data, 
										array('id_coa'	=> $data['id_coa']
												));

					$ls_response = "Data Tersimpan";
				}
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	
	
}
?>